package controllers.customer;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.CommentService;
import controllers.AbstractController;
import domain.Comment;

@Controller
@RequestMapping("/comment/customer")
public class CommentCustomerController extends AbstractController {
	@Autowired
	private CommentService commentService;

	public CommentCustomerController() {
		super();
	}

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam int paintingId) {
		ModelAndView result;
		Comment comment;
		Boolean isNew;
		Boolean isResponse;

		comment = commentService.create(paintingId);
		isNew = true;
		isResponse = false;

		result = new ModelAndView("comment/create");
		result.addObject("requestURI", "comment/create.do?paintingId="
				+ paintingId);
		result.addObject("comment", comment);
		result.addObject("isNew", isNew);
		result.addObject("isResponse", isResponse);
		result.addObject("backURI", "/comment/list.do?paintingId=" + paintingId);
		return result;
	}

	@RequestMapping(value = "/respond", method = RequestMethod.GET)
	public ModelAndView respond(@RequestParam int commentId) {
		ModelAndView result;
		Comment parentComment;
		Comment comment;
		Boolean isNew;
		Boolean isResponse;

		parentComment = commentService.findOne(commentId);
		comment = commentService.create(parentComment.getPainting().getId());
		comment.setParent(parentComment);

		isNew = false;
		isResponse = true;

		result = new ModelAndView("comment/create");
		result.addObject("requestURI", "comment/create.do");
		result.addObject("comment", comment);
		result.addObject("isNew", isNew);
		result.addObject("isResponse", isResponse);
		result.addObject("backURI", "comment/edit.do?commentId=" + commentId);
		return result;
	}

	// Edition
	@RequestMapping(value = "/create", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid Comment comment, BindingResult binding) {
		ModelAndView result;

		if (binding.hasErrors()) {
			result = createEditModelAndView(comment);
		} else {
			try {

				commentService.save(comment);
				result = new ModelAndView(
						"redirect:/comment/list.do?paintingId="
								+ comment.getPainting().getId());

			} catch (Throwable oops) {

				result = createEditModelAndView(comment, "comment.commit.error");
			}
		}
		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(Comment comment) {
		ModelAndView result;

		result = createEditModelAndView(comment, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Comment comment,
			String message) {
		ModelAndView result;

		result = new ModelAndView("comment/create");

		result.addObject("comment", comment);
		result.addObject("message", message);

		return result;
	}

}
